﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BitPongTetrisAlgorithm;
using System.IO;

namespace BitPongTetrisUI
{
    public static class BitPongOutput
    {
        public static Canvas canvas;
        public static int topLeftX;
        public static int topLeftY;
        public static int blockSize;
        public static int counter = 0;

        public static Brush red = Brushes.Red;
        public static Brush white = Brushes.White;
        public static Brush black = Brushes.Black;
        public static Brush gray = Brushes.Gray;

        public static void PrintBlock(int row, int col, string text, Brush background, Brush foreGround, Brush border)
        {
            Rectangle rect = new Rectangle()
            {
                Width = blockSize,
                Height = blockSize,
                Stroke = border,
                StrokeThickness = 1,
            };

            TextBlock textBlock = new TextBlock()
            {
                Text = text,
                TextAlignment = System.Windows.TextAlignment.Center,
                FontSize = blockSize * 0.6,
                LineHeight = blockSize,
                Background = background,
                Width = blockSize,
                Height = blockSize,
                Foreground = foreGround,                
            };            

            int x = topLeftX + col * blockSize;
            int y = topLeftY + row * blockSize;

            Canvas.SetLeft(textBlock, x);
            Canvas.SetTop(textBlock, y);
            canvas.Children.Add(textBlock);

            Canvas.SetLeft(rect, x);
            Canvas.SetTop(rect, y);
            canvas.Children.Add(rect);
        }

        public static string PrintField(long field, int dx)
        {
            StringBuilder result = new StringBuilder();
            using (StringWriter writer = new StringWriter(result))
            {
                int Columns = 4;
                int Rows = 16;
                int row = 0;
                int col = 0;

                for (int i = 0; i < 4; i++)
                {
                    PrintBlock(-1, i, "c" + i, white, black, white);
                }

                writer.WriteLine("dx = {0}", dx);
                PrintBlock(0, -1, "r0", white, black, white);

                for (int bit = 0; bit < 64; bit++)
                {
                    row = bit / Columns;
                    col = bit % 4;
                    writer.Write("{0}", ((field >> bit) & 1) == 1 ? '*' : '.');

                    long bitValue = ((field >> bit) & 1);
                    Brush back = bitValue == 1 ? red : white;
                    Brush fore = bitValue == 1 ? white : black;
                    PrintBlock(row, col, bitValue.ToString(), back, fore, black);

                    if (bit % 4 == 3)
                    {
                        writer.WriteLine();
                        if (bit < 63)
                        {
                            PrintBlock(row + 1, -1, "r" + (row + 1), white, black, white);
                        }
                    }
                }
            }

            return result.ToString();
        }

        internal static void PlayGame(string inputData, Canvas canvas)
        {
            BitPongOutput.canvas = canvas;
            BitPongOutput.topLeftX = 40;
            BitPongOutput.topLeftY = 40;
            BitPongOutput.blockSize = 20;
            int moveX = 6 * blockSize;
            int moveY = 18 * blockSize;

            BitPongTetris.MainAlgorithm(inputData, (field, dx) =>
            {
                PrintField(field, dx);
                DrawTurnCounter();

                topLeftX += moveX;
                if (topLeftX + moveX + 40 > canvas.Width)
                {
                    canvas.Width += moveX;
                }
            });
        }

        public static void DrawTurnCounter()
        {
            int blockSize = BitPongOutput.blockSize * 2;

            Ellipse circle = new Ellipse()
            {
                Width = blockSize,
                Height = blockSize,
                Stroke = black,
                StrokeThickness = 1,
            };

            TextBlock textBlock = new TextBlock()
            {
                Text = (counter++).ToString(),
                TextAlignment = System.Windows.TextAlignment.Center,
                FontSize = blockSize * 0.6,
                LineHeight = blockSize,
                Width = blockSize,
                Height = blockSize,
                Foreground = black,
            };

            int x = topLeftX + ((5 * BitPongOutput.blockSize) / 2) - blockSize / 2 - BitPongOutput.blockSize/2;
            int y = topLeftY + 17 * BitPongOutput.blockSize;

            Canvas.SetLeft(textBlock, x);
            Canvas.SetTop(textBlock, y);
            canvas.Children.Add(textBlock);

            Canvas.SetLeft(circle, x);
            Canvas.SetTop(circle, y);
            canvas.Children.Add(circle);
        }
    }
}
